import {useQuasar} from "quasar";
import APIClient, {conf} from "src/utils/APIClient";
import {useRouter} from "vue-router";
import eventBus from "src/utils/eventBus";
import {useSysStore} from "stores/sysStore";

const router = useRouter()
const store = useSysStore();

// 创建通知配置
export const tips = {
  success: (quasar: any, message: string) => quasar.notify({
    message: `${message}`,
    type: 'positive' as const,
    position: 'top' as const,
  }),
  warning: (quasar: any, message: string) => quasar.notify({
    message: `${message}`,
    type: 'warning' as const,
    position: 'top' as const,
  }),
  error: (quasar: any, message: string) => quasar.notify({
    message: `${message}`,
    type: 'negative' as const,
    position: 'top' as const,
  }),
  info: (quasar: any, message: string) => quasar.notify({
    type: 'info' as const,
    position: 'top' as const,
    message: `${message}`
  })
};

export const loginOut = () => {
  localStorage.clear();
  location.href = '/#/login'
  eventBus.$emit("loginExpire");
}

//-------------------------上传单个文件-------------------------
export const uploadFileToService = async () => {
  let token = localStorage.getItem('token');
  let fileInput = document.getElementById('file');
  let file = fileInput.files[0]; // 获取文件
  let formData = new FormData();
  formData.append('file', file); // 将文件添加到formData对象中

  await APIClient.uploadFile('/user/file/convert', formData).then(res => {
    // console.log(res.data.FileList)
    // window.open(`${conf.host}/user/image/get?path=${res.data.FileList[0]}&token=${token}`)
    eventBus.$emit('uploadFileDown', res.data);
  }).finally(() => {
    (document.getElementById('file') as HTMLDivElement).value = null;
  })
}

//-------------------------上传多个文件-------------------------
export const uploadFilesToService = async () => {
  let token = localStorage.getItem('token');
  let fileInput:HTMLElement | null = document.getElementById('files');
  let files = (fileInput as HTMLElement).files; // 获取文件
  let formData = new FormData();
  for (let i = 0; i < files.length; i++) {
    formData.append('files', files[i]);
  }
  await APIClient.uploadFile('/user/upload_files', formData).then(res => {
    eventBus.$emit('uploadFilesBack', res.data);
  }).finally(() => {
    (document.getElementById('files') as HTMLDivElement).value = null;
  })
}

//-------------------------用户行为记录-------------------------
/**
 * view_agent:打开agent问答
 * view_monitoring: 打开监控
 *
 */
interface Behavior {
  channel_instance_id?: string // 房间实例ID
  operation_type_id?: string // 操作类型ID
  operation_content?: string // 操作相关的内容
  note?: string // 备注
}
export const userBehavior = async (behavior: Behavior) => {
  if (store.RTCConfig.channel_instance_id) {
    behavior['channel_instance_id'] = store.RTCConfig.channel_instance_id;
  }
  await APIClient.post('/user/video/user_behavior/insert', behavior).catch((e=>{
    console.error('行为记录失败:', e);
  }));
}

export const isWorkTime = ()=> {
    const now = new Date();
    const day = now.getDay();
    const hours = now.getHours();
    const minutes = now.getMinutes();
 
    // 检查是否为工作日（星期一至星期五）
    if (day >= 1 && day <= 5) {
        // 检查是否在工作时间（9:00到12:00，13:00到17:00）
        if ((hours === 9 && minutes >= 0) || 
            (hours > 9 && hours < 12) || 
            (hours === 13 && minutes >= 0) || 
            (hours > 13 && hours < 17)) {
            return true;
        }
    }
    return false;
}

export const nowDate = ()=> {
  let today = new Date();
  let year = today.getFullYear();
  let month = today.getMonth() + 1;
  let day = today.getDate();
  let weekday = today.getDay();
  let weekdays = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
  let dayOfWeek = weekdays[weekday];
  return year+'年'+month+'月'+day+'日'+' '+dayOfWeek
}

export const formatDate = (dateStr)=> {
  const d = new Date(dateStr);
  const y = d.getFullYear();
  const m = (d.getMonth() + 1).toString().padStart(2, '0');
  const day1 = d.getDate().toString().padStart(2, '0');
  const h = d.getHours().toString().padStart(2, '0');
  const min = d.getMinutes().toString().padStart(2, '0');
  const s = d.getSeconds().toString().padStart(2, '0');
  return `${y}-${m}-${day1} ${h}:${min}:${s}`;
};